package com.ruoyi.bzjgl.controller;

import com.ruoyi.bzjgl.domain.Bzjtable;
import com.ruoyi.bzjgl.domain.FileTable;
import com.ruoyi.bzjgl.service.IBzjtableService;
import com.ruoyi.bzjgl.service.IForfeitureOfDepositService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/confiscate/forfeitureOfDeposit")
public class ForfeitureOfDepositController extends BaseController {

    @Autowired
    private IForfeitureOfDepositService forfeitureOfDepositService;
    @Autowired
    private IBzjtableService bzjtableService;

    /**
     * 查询没收查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = forfeitureOfDepositService.selectMsglList(bzjtable);
        return getDataTable(list);
    }

    @GetMapping("/{xh}")
    public AjaxResult getInfo(@PathVariable("xh") String xh)
    {
        return success(forfeitureOfDepositService.selectMsByXh(xh));
    }

    @GetMapping("/msshList")
    public TableDataInfo msshList(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = forfeitureOfDepositService.selectMsshList(bzjtable);
        return getDataTable(list);
    }

    @GetMapping("/mshzList")
    public TableDataInfo mshzList(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = forfeitureOfDepositService.selectMshzList(bzjtable);
        return getDataTable(list);
    }

    //没收附件上传地址
    @PostMapping("/uploadMsFile")
    public AjaxResult uploadMsFile(@RequestParam("file")  MultipartFile file) throws Exception{
        try{
            // 上传文件路径
            String fileFirstPath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileLastPath = FileUploadUtils.upload(fileFirstPath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION).replace("/upload","");
            AjaxResult ajax = AjaxResult.success();
            HashMap<String,Object> fileTable = new HashMap<>();
            fileTable.put("fileFirstPath", fileFirstPath);
            fileTable.put("fileLastPath", fileLastPath);
            fileTable.put("newFileName", FileUtils.getName(fileLastPath));
            fileTable.put("originalFilename", file.getOriginalFilename());
            List<HashMap<String,Object>> fileTables = new ArrayList<>();
            fileTables.add(fileTable);
            ajax.put("fileTables",fileTables);
            return ajax;
        }catch (Exception e){
            return   AjaxResult.error(e.getMessage());
        }
    }

    //文件下载
    @GetMapping("/downLoadFile")
    public void downLoadFile(String wjdz, HttpServletResponse response) throws Exception{
        try
        {
            // 下载名称
            String downloadName = StringUtils.substringAfterLast(wjdz, "/");
            response.setCharacterEncoding("utf-8");
            response.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            FileUtils.writeBytes(wjdz, response.getOutputStream());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    //提交审核
    @PostMapping("/updateMsshzt")
    public AjaxResult updateMsshzt(@RequestBody  Bzjtable bzjtable)
    {
        return  toAjax(forfeitureOfDepositService.updateMsshzt(bzjtable));
    }

    @PostMapping("/mssh")
    public AjaxResult mssh(@RequestBody Bzjtable bzjtable)
    {
        return  toAjax(forfeitureOfDepositService.mssh(bzjtable));
    }

    @PostMapping("/shBack")
    public AjaxResult shBack(@RequestBody Bzjtable bzjtable)
    {
        return  toAjax(forfeitureOfDepositService.shBack(bzjtable));
    }


    @GetMapping("/fjList")
    public TableDataInfo fjList(String xh)
    {
        List<FileTable> list = forfeitureOfDepositService.selectFjByXh(xh);
        return getDataTable(list);
    }
}
